﻿using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OrmAuto;
using System.Data;
using System.Data.Common;
using System;

public partial class Controls_myAnnuncioSqlAcq : System.Web.UI.UserControl
{

    protected void Page_Load()
    {
        if (!IsPostBack)
            Inizializza();
    }//fM

    public void Inizializza()
    {

        if (!Page.User.Identity.IsAuthenticated)
            Response.Redirect(System.Web.Security.FormsAuthentication.LoginUrl);

        string uName = Page.User.Identity.Name;

        Querier query = new Querier();

        using (var DAL = query.getDAL())
        {
            var controlUserID = this.FormView1.FindControl("UserIDLabel") as Label;
            var UserId = DAL.Users.Where(u => u.UserName == uName).Select(u => u.UserId).FirstOrDefault();
            if (UserId < 1) Response.Redirect("~");

            return;
            //marche
            var controlMarche = this.FormView1.FindControl("FkMarcheDrop") as DropDownList;
            var dati = DAL.MarcheAuto.ToArray();
            var items = dati.Select(d => new ListItem { Text = d.Marca, Value = d.ID.ToString() }).ToArray();
            controlMarche.Items.AddRange(items);

            //
            DropDownList control = this.FormView1.FindControl("FkTipiDrop") as DropDownList;
            var items2 = DAL.TipiAuto.ToArray()
                    .Select(d => new ListItem { Text = d.TipoIT, Value = d.ID.ToString() }).ToArray();
            control.Items.AddRange(items2);
        }

    }//fM

    public void NuovoAnnuncio()
    {
        this.FormView1.Caption = "Nuovo annuncio";
        this.FormView1.ChangeMode(FormViewMode.Insert);

        TextBox priceTxt = this.FormView1.FindControl("myTextPrezzo") as TextBox;
        if (priceTxt != null)
            priceTxt.Attributes.Add("onBlur", "Javascript:checkMaxValue(this,0,900000);");
    }//fM

    public void AggiornaAnnuncio(int pID)
    {
        this.FormView1.Caption = "In Aggiornamento";
        this.FormView1.ChangeMode(FormViewMode.Edit);

        QuerierMarcheModelli qMM = new QuerierMarcheModelli();

        int idMarca;
        using (var DAL = qMM.getDAL())
        {
            idMarca = DAL.ANNAcquisto.Where(a => a.ID == pID).Select(a => a.MarcheAuto.ID).FirstOrDefault();
        }

        SqlDataSource_Modelli.SelectParameters["MarcaID"].DefaultValue = idMarca.ToString();
        //SqlDataSource_Modelli.DataBind();

        this.SqlDataSource_AnnAcquisto.SelectParameters["ID"].DefaultValue = pID.ToString();
        //this.SqlDataSource_AnnVendita.DataBind();
        this.FormView1.DataBind();

        TextBox priceTxt = this.FormView1.FindControl("myTextPrezzo") as TextBox;
        if (priceTxt != null)
            priceTxt.Attributes.Add("onBlur", "Javascript:checkMaxValue(this,0,900000);");

    }//fM

    protected void FormView1_ItemInserting(object sender, FormViewInsertEventArgs e)
    {
        //var a = e.Values[1];
        //string t ="" ;		  
        //foreach (var key in e.Values.Keys)
        //{
        //     t += " ,\n "+ key +" : "+ e.Values[key]; 
        //}
    }//fM

    protected void FormView1_ItemInserted(object sender, FormViewInsertedEventArgs e)
    {
        if (e.Exception == null)
        {
            myLiteralMsg.Text = "<b>Dati salvati !  </b> ( " + DateTime.Now.ToLongTimeString();
            //e.KeepInInsertMode = true  ;
            //Inizializza(); 
            //var ID =e.Values["ID"]; 
            //this.SqlDataSource_AnnVendita.SelectParameters["ID"].DefaultValue  = ID.ToString() ;  
        }
        else
        {
            e.ExceptionHandled = true;
            myLiteralMsg.Text = "<b>Errore : </b> " + e.Exception.Message;
        }
    }//fM

    protected void FormView1_ItemUpdating(object sender, FormViewUpdateEventArgs e)
    {
        //var a = e.NewValues[1];
        //string t = "";
        //foreach (var key in e.NewValues.Keys)
        //{
        //  t += " ,\n " + key + " : " + e.NewValues[key];
        //}

        // Non commentare
        if (e.NewValues["FkModello"] != null)
        {
            var modelli = e.NewValues["FkModello"].ToString();
            var arr = modelli.Split(new char[] { ':' });
            if (arr != null)
            {
                e.NewValues["FkModello"] = arr[0];
            }
        }

    }//fM

    protected void FormView1_ItemUpdated(object sender, FormViewUpdatedEventArgs e)
    {
        if (e.Exception == null)
        {
            myLiteralMsg.Text = "<b>Dati salvati !  </b> ( " + DateTime.Now.ToLongTimeString();
            //Inizializza();
            //var ID =e.Values["ID"]; 
            //this.SqlDataSource_AnnVendita.SelectParameters["ID"].DefaultValue  = ID.ToString() ;  
        }
        else
        {
            e.ExceptionHandled = true;
            myLiteralMsg.Text = "<b>Errore : </b> " + e.Exception.Message;
        }

    }//fM



}//fC
